package com.pri.chat.fragment.daohang;

import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.netease.nim.uikit.business.session.viewholder.GlideBlurTransformation;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.UserBean;
import com.netease.nim.uikit.net.entity.WishListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.ReleaseWishAcctivity;
import com.pri.chat.activity.WishActivity;
import com.pri.chat.model.UserModel;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.view.ImageViewPlus;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.fragment.FragmentLazy;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/3
 */
public class WishFragment extends FragmentLazy {
    Unbinder unbinder;
    @BindView(R.id.stl_wish)
    SlidingTabLayout stlWish;
    @BindView(R.id.vp_wish)
    ViewPager vpWish;
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.iv_logo)
    ImageViewPlus ivLogo;
    @BindView(R.id.rl_logo)
    RelativeLayout rlLogo;
    @BindView(R.id.tv_me_name)
    TextView tvMeName;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.rl_center)
    RelativeLayout rlCenter;
    @BindView(R.id.rl_wish_qiang)
    ImageView rlWishQiang;
    @BindView(R.id.tv_vip)
    TextView tvVip;
    @BindView(R.id.iv_release)
    ImageView iv_release;


    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_wish_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initLayout();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserInfo();
    }

    private void setUserInfo() {
        SubscriberOnNextListener<BaseBean<UserBean>> onNextListener = baseBean -> {
            UserBean userBean = baseBean.getData();
            tvMeName.setText(userBean.getMemberInfo().getNcName());
            Glide.with(mContext).load(userBean.getMemberInfo().getHeadPic()).into(ivLogo);
            RequestOptions options = new RequestOptions().fitCenter()
                    .transform(new GlideBlurTransformation(mContext));
            Glide.with(this).load(userBean.getMemberInfo().getHeadPic()).apply(options).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    ivBg.setImageDrawable(resource);
                }
            });
            if (userBean.getMemberInfo().getMemberLevel().equals("否")) {
                tvVip.setVisibility(View.GONE);
            } else {
                tvVip.setVisibility(View.INVISIBLE);
            }
        };
        UserModel userModel = new UserModel();
        userModel.setMemberId(SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(userModel));
        HttpMethods.getInstance().getMemberCenterInfo(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        StatusBarUtil.setTranslucentForImageViewInFragment(mContext, 0, getView());
        StatusBarUtil.setLightMode(mContext);
    }

    public void updateWish(WishListBean obj) {
        if (arrayList.get(1) != null) {
            ((WishAllFragment) arrayList.get(1)).updateWish(obj);
        }
    }

    public void deteleWish(WishListBean obj) {
        if (arrayList.get(1) != null) {
            ((WishAllFragment) arrayList.get(1)).updateWish(obj);
        }
    }

    private String[] mTitle = new String[]{"愿望金", "愿望墙"};
    private int selectIndex = 0;
    ArrayList<Fragment> arrayList = new ArrayList<>();

    private void initLayout() {
        arrayList.add(WishMsgFragment.getInstance(0));
//        arrayList.add(WishDoneFragment.getInstance(1));
//        arrayList.add(WishDoneFragment.getInstance(3));
        arrayList.add(WishAllFragment.getInstance(1));
//        arrayList.add(WishAllFragment.getInstance(2));
        stlWish.setViewPager(vpWish, mTitle, mContext, arrayList);
        stlWish.getTitleView(0).setTextSize(20);
        stlWish.getTitleView(1).setTextSize(14);
//        stlWish.getTitleView(2).setTextSize(14);
        stlWish.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlWish.getTitleView(position).setTextSize(20);
                        stlWish.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlWish.getTitleView(i).setTextSize(14);
                        stlWish.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vpWish.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlWish.getTitleView(position).setTextSize(20);
                        stlWish.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlWish.getTitleView(i).setTextSize(14);
                        stlWish.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlWish.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }


    @Override
    protected void initData() {

    }

    @OnClick({R.id.rl_wish_qiang, R.id.iv_release})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.rl_wish_qiang) {
            RxActivityTool.skipActivity(mContext, WishActivity.class);
        } else if (v.getId() == R.id.iv_release) {
            RxActivityTool.skipActivity(mContext, ReleaseWishAcctivity.class);
        }
    }
}
